package controllers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import play.mvc.Controller;


import models.Usuario;

public class visualizarUsuarios extends Application {

	private static Connection conexao;
	private static Statement comando;
	
	/*************************************************************/
	
	protected static void conectar() throws SQLException {  
	    		  
		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
		}

			conexao = DriverManager.getConnection(  
			        "jdbc:postgresql://localhost:5432/SGRv2",  
			        "postgres",  
			        "senha");
		
			comando =  conexao.createStatement();
	}

	/***********************************************************/
	
	public static void visualizarUsuarios() throws SQLException {
		
		String idusuario = "";
		int x = 0;
		try
		{		
			idusuario =  request.querystring.toString();

			idusuario = idusuario.substring(5);
			x = Integer.parseInt(idusuario);
			
		}
		catch (Exception e)
		{
			
		}		
		 if(x > 0)
		 {
			 deletar(x);
		 }
		
		List<Usuario> contatos = new ArrayList<Usuario>();
		
		contatos = Usuario.recuperarPessoas();
		
		render(contatos);
	}	
	
	/***********************************************************************************/
	
	public static void deletar( int usuarioid) throws SQLException
	{
		conectar();
				
		comando.executeUpdate("delete from pessoa where idusuario = " + usuarioid );
		visualizarUsuarios();
		
	}
	
	/*************************************************************/
}
